import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution399 {

    public static void main(String args[]){
        Solution399 s = new Solution399();
    }

    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        HashMap<String, Integer> vmp = new HashMap<>();
        HashMap<String, HashMap<String, Double>> emp = new HashMap<>();

        for (int i = 0; i < equations.length; i++) {
            String l = equations[i][0];
            String r = equations[i][1];
            double v = values[i];

            vmp.put(l, 1);
            vmp.put(r, 1);
            addE(emp, l, r, v);
            addE(emp, r, l, 1.0 / v);
        }

        int n = vmp.keySet().size();
        String[] vs = new String[n];
        vs = vmp.keySet().toArray(vs);
        for(int k = 0 ; k < n ; k ++){
            for(int i = 0 ; i < n ; i ++){
                for(int j = 0 ; j < n ; j ++){
                    if(linked(emp, vs[i], vs[k]) && linked(emp,vs[k], vs[j])){
                        addE(emp, vs[i], vs[j], emp.get(vs[i]).get(vs[k]) * emp.get(vs[k]).get(vs[j]));
                    }
                }
            }
        }
        double[] ret = new double[queries.length];
        for(int i = 0 ; i < queries.length ; i ++){
            String l = queries[i][0];
            String r = queries[i][1];
            if(emp.containsKey(l) && emp.get(l).containsKey(r)){
                ret[i] = emp.get(l).get(r);
            }else{
                ret[i] = -1.0;
            }
        }
        return ret;
    }

    public boolean linked(HashMap<String, HashMap<String, Double>> emp, String l, String r){
        if(emp.containsKey(l) && emp.get(l).containsKey(r)){
            return true;
        }
        return false;
    }

    public void addE(HashMap<String, HashMap<String, Double>> emp, String l, String r, double v){
        if(!emp.containsKey(l)){
            emp.put(l, new HashMap<>());
        }
        emp.get(l).put(r, v);
    }
}
